package com.guanyong.services;

import com.guanyong.dao.IMessageDaoMapper;
import com.guanyong.model.MessageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/6/6.
 */
@Service
public class MessageService {


    @Autowired
    IMessageDaoMapper iMessageDaoMapper;

    @Autowired
    ObjectClassTypeService objectClassTypeService;

    public MessageModel GetMessageModelById(Integer fid){

        return  iMessageDaoMapper.selectByPrimaryKey(fid);
    }

    public HashMap<String,Object> GetMessageList(
            MessageModel messageModel,
            Integer pno,
            Integer pagesize,
            Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iMessageDaoMapper
                    .SelectMessgaeListCount(messageModel);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<MessageModel> priceTagModelList
                =iMessageDaoMapper.SelectMessgaeList(messageModel,(pno-1)*pagesize,
                pagesize);
        hashMap.put("list",priceTagModelList);
        return hashMap;
    }


    public HashMap<String,Object> GetUserMessageList(
            String userid,
            Date intime,
            Integer pno,
            Integer pagesize,
            Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iMessageDaoMapper
                    .SelectUserMessgaeListCount(userid,intime);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<MessageModel> priceTagModelList
                =iMessageDaoMapper.SelectUserMessgaeList(userid,intime,(pno-1)*pagesize,
                pagesize);
        for (MessageModel messageModel :priceTagModelList){

            if (messageModel.getfType()==0){
                Object object= objectClassTypeService.GetObject(1,
                        messageModel.getfUrl());
                if (object!=null) {
                    messageModel.setfObject(object);
                }
            }


        }

        hashMap.put("list",priceTagModelList);
        return hashMap;
    }


    public Boolean GetNewMessage(
            String userid,
            Date intime){
        HashMap<String,Object> hashMap=new HashMap();

       Integer res  =iMessageDaoMapper.SelectNewMessgae(userid,intime);

        return res==0;
    }

    public  Integer UpdateMessgae(MessageModel messageModel){
                return iMessageDaoMapper.update(
                        messageModel
                );
    }

    public Integer AddMessage(MessageModel messageModel){
        return  iMessageDaoMapper.insert(messageModel);
    }

}
